package com.pilipala.mapper;

import com.github.pagehelper.Page;
import com.pilipala.dto.BlogSearchDTO;
import com.pilipala.dto.SearchDTO;
import com.pilipala.entity.Blog;
import com.pilipala.vo.BlogVO;
import org.apache.ibatis.annotations.*;

@Mapper
public interface BlogMapper {

    void insert(Blog blog);

    /**
     * 无条件获取所有Blog
     * @param blogSearchDTO 页码、页大小、选择的条件
     * @return 分页后的结果
     */
    Page<Blog> listByCondition(BlogSearchDTO blogSearchDTO);





    /**
     * 分页搜索
     * @param searchDTO 包括页码、页大小、搜索关键字、排序类型
     * @return 分页后的数据
     */
    Page<Blog> search(SearchDTO searchDTO);


    /**
     * 根据博客Id获取博客
     * @param id 博客Id
     * @return 返沪博客数据
     */
    @Select("select * from blog where id = #{id}")
    BlogVO getById(Long id);

    @Update("update blog set commit_number = commit_number + 1 where id = #{blogId}")
    void incrementComment(Long blogId);

    /**
     * 无条件获取所有Blog
     * @return 分页后的结果
     */
    @Select("select * from blog")
    Page<Blog> list();

    @Delete("delete from blog where id = #{id}")
    void delete(Long id);

    /**
     * 更新博客
     * @param blog 博客
     */
    void update(Blog blog);

    @Select("select blog_id from favorites where user_id = #{userId}")
    Page<Long> getCollectionByUserId(Long userId);

    @Select("select blog_id from favorites where user_id = #{userId} and blog_id = #{blogId}")
    Long getCollectionPair(Long userId, Long blogId);

    @Insert("insert into favorites (user_id,blog_id) values (#{userId},#{blogId})")
    void addCollection(Long userId, Long blogId);

    @Delete("delete from favorites where user_id = #{userId} and blog_id = #{blogId}")
    void deleteCollection(Long userId, Long blogId);
}
